package com.lanvideo.dao;

import com.lanvideo.entity.UserFavorite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户收藏数据访问层
 */
@Repository
public class UserFavoriteDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 添加收藏
     */
    public int addFavorite(Integer userId, Integer videoId) {
        String sql = "INSERT INTO user_favorite (user_id, video_id) VALUES (?, ?)";
        return jdbcTemplate.update(sql, userId, videoId);
    }

    /**
     * 取消收藏
     */
    public int removeFavorite(Integer userId, Integer videoId) {
        String sql = "DELETE FROM user_favorite WHERE user_id = ? AND video_id = ?";
        return jdbcTemplate.update(sql, userId, videoId);
    }

    /**
     * 检查是否已收藏
     */
    public boolean isFavorited(Integer userId, Integer videoId) {
        String sql = "SELECT COUNT(*) FROM user_favorite WHERE user_id = ? AND video_id = ?";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class, userId, videoId);
        return count != null && count > 0;
    }

    /**
     * 获取用户的收藏列表
     */
    public List<UserFavorite> getUserFavorites(Integer userId) {
        String sql = "SELECT uf.*, v.title as video_title, v.intro as video_intro, " +
                "v.tags as video_tags, v.upload_time, v.view_count, u.name as uploader_name " +
                "FROM user_favorite uf " +
                "LEFT JOIN video v ON uf.video_id = v.id " +
                "LEFT JOIN user u ON v.uploader_id = u.id " +
                "WHERE uf.user_id = ? AND v.status = 1 " +
                "ORDER BY uf.favorite_time DESC";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UserFavorite.class), userId);
    }

    /**
     * 获取视频的收藏数量
     */
    public int getVideoFavoriteCount(Integer videoId) {
        String sql = "SELECT COUNT(*) FROM user_favorite WHERE video_id = ?";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class, videoId);
        return count != null ? count : 0;
    }

    /**
     * 获取用户的收藏数量
     */
    public int getUserFavoriteCount(Integer userId) {
        String sql = "SELECT COUNT(*) FROM user_favorite WHERE user_id = ?";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class, userId);
        return count != null ? count : 0;
    }

    /**
     * 删除收藏
     */
    public int deleteFavorite(Integer id) {
        String sql = "DELETE FROM user_favorite WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    /**
     * 根据ID获取收藏
     */
    public UserFavorite getFavoriteById(Integer id) {
        String sql = "SELECT uf.*, v.title as video_title, v.intro as video_intro, " +
                "v.tags as video_tags, v.upload_time, v.view_count, u.name as uploader_name " +
                "FROM user_favorite uf " +
                "LEFT JOIN video v ON uf.video_id = v.id " +
                "LEFT JOIN user u ON v.uploader_id = u.id " +
                "WHERE uf.id = ?";
        List<UserFavorite> favorites = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UserFavorite.class), id);
        return favorites.isEmpty() ? null : favorites.get(0);
    }
}
